<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    reservationData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    reservationData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
    },
    setOptions(arr) {
      let dateArr = [];
      let dataArr = [];

      if(arr.length > 0){
        arr.forEach(item=>{
          dateArr.push(item['date']);
          dataArr.push(item['paymentAmount'])
        })
      }

      this.chart.setOption({
         title: {
            text: '近7日门诊充值趋势'
          },
        xAxis: {
          name:'日期',
          data: dateArr,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        yAxis: {
          name:'单位(元)',
          axisTick: {
            show: false
          },
          axisLine: {
                     show: false // 隐藏Y轴的竖线
                 }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },

        legend: {
          data: ['expected', 'actual']
        },
        series: [{
          name: 'expected', itemStyle: {
            normal: {
              color: '#FDA95C',
              lineStyle: {
                color: '#FDA95C',
                width: 5
              }
            }
          },
          smooth: true,
          type: 'line',
          data: dataArr,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }]
      })
    }
  }
}
</script>
